package com.study.o2o.web.frontend;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.study.o2o.dto.shopExecution;
import com.study.o2o.entity.Area;
import com.study.o2o.entity.Shop;
import com.study.o2o.entity.shopCategory;
import com.study.o2o.service.areaService;
import com.study.o2o.service.shopCategoryService;
import com.study.o2o.service.ShopService;
import com.study.o2o.util.httpServletRequestUtil;

@Controller
@RequestMapping("/frontend")
@Api(description = "商铺列表")
public class shopListController {
	@Autowired
	private areaService areaService;
	@Autowired
	private shopCategoryService shopCategoryService;
	@Autowired
	private ShopService shopService;
	/**
	 * 返回商品列表中的ShopCategory列表(二级或者一级), 以及区域信息列表
	 * @param request
	 * @return
	 */
	@ApiOperation(value = "获取商品列表中的ShopCategory列表(二级或者一级), 以及区域信息列表")
	@RequestMapping(value = "/listshopspageinfo", method = RequestMethod.GET)
	@ResponseBody
	private Map<String,Object> listShopsPageInfo(HttpServletRequest request){
		Map<String, Object> modelMap = new HashMap<String, Object>();
		//尝试从前端请求中获取parentId
		long parentId = httpServletRequestUtil.getLong(request, "parentId");
		List<shopCategory> shopCategoryList = null;
		if(parentId!=-1) {
			//如果parentId存在, 则取出该一级shopCategory下的二级shopCategory列表
			try {
				shopCategory shopCategoryCondition  = new shopCategory();
				shopCategory parent = new shopCategory();
				parent.setShopCategoryId(parentId);
				shopCategoryCondition.setParent(parent);
				shopCategoryList = shopCategoryService.getShopCategoryList(shopCategoryCondition);
			} catch (Exception e) {
				modelMap.put("success", false);
				modelMap.put("errMsg", e.getMessage());
			}
		}else {
			try {
				//如果parentId不存在,则取出所有一级shopCategory(用户在首页选择的是全部列表)
				shopCategoryList = shopCategoryService.getShopCategoryList(null);
			} catch (Exception e) {
				modelMap.put("success", false);
				modelMap.put("errMsg", e.getMessage());
			}
		}
		modelMap.put("shopCategoryList",shopCategoryList);
		List<Area> areaList = null;
		try {
			//获取区域列表信息
			areaList = areaService.getAreaList();
			modelMap.put("areaList", areaList);
			modelMap.put("success", true);
			return modelMap;
		} catch (Exception e) {
			modelMap.put("success", false);
			modelMap.put("errMsg", e.getMessage());
		}
		return modelMap;
	}
	/**
	 * 获取指定查询条件下的店铺列表
	 * @param request
	 * @return
	 */
	@ApiOperation(value = "获取指定查询条件下的店铺列表")
	@RequestMapping(value = "/listshops",method = RequestMethod.GET)
	@ResponseBody
	private Map<String, Object> listShops(HttpServletRequest request){
		Map<String, Object> modelMap = new HashMap<>();
		//获取页码
		int pageIndex = httpServletRequestUtil.getInt(request, "pageIndex");
		//获取一页需要显示的数据条数
		int pageSize = httpServletRequestUtil.getInt(request, "pageSize");
		//非空判断
		if((pageIndex > -1)&&(pageSize > -1)) {
			//尝试获取一级类别Id
			long parentId = httpServletRequestUtil.getLong(request, "parentId");
			//尝试获取特定二级类别Id
			long shopCategoryId = httpServletRequestUtil.getLong(request, "shopCategoryId");
			//尝试获取区域Id
			int areaId = httpServletRequestUtil.getInt(request, "areaId");
			//尝试获取模糊查询的值
			String shopName = httpServletRequestUtil.getString(request, "shopName");
			//获取组合之后的查询条件
			Shop shopCondition = compactShopCondition4Search(parentId,shopCategoryId, areaId, shopName);
			//根据查询条件和分页信息获取店铺列表, 并返回总数
			shopExecution se = shopService.getShopList(shopCondition, pageIndex, pageSize);
			modelMap.put("shopList", se.getShopList());
			modelMap.put("count", se.getCount());
			modelMap.put("success",true);
		}else {
			modelMap.put("success", false);
			modelMap.put("errMsg","empty pageSize or pageIndex");
		}
		return modelMap;
	}
	/**
	 * 组合查询条件，并将条件封装到ShopCondition对象里返回
	 * 
	 * @param parentId
	 * @param shopCategoryId
	 * @param areaId
	 * @param shopName
	 * @return
	 */
	private Shop compactShopCondition4Search(long parentId, long shopCategoryId, int areaId, String shopName) {
		Shop shopCondition = new Shop();
		if (parentId != -1L) {
			// 查询某个一级ShopCategory下面的所有二级ShopCategory里面的店铺列表
			shopCategory childCategory = new shopCategory();
			shopCategory parentCategory = new shopCategory();
			parentCategory.setShopCategoryId(parentId);
			childCategory.setParent(parentCategory);
			shopCondition.setShopCategory(childCategory);
		}
		if (shopCategoryId != -1L) {
			// 查询某个二级ShopCategory下面的店铺列表
			shopCategory shopCategory = new shopCategory();
			shopCategory.setShopCategoryId(shopCategoryId);
			shopCondition.setShopCategory(shopCategory);
		}
		if (areaId != -1L) {
			// 查询位于某个区域Id下的店铺列表
			Area area = new Area();
			area.setAreaId(areaId);
			shopCondition.setArea(area);
		}

		if (shopName != null) {
			// 查询名字里包含shopName的店铺列表
			shopCondition.setShopName(shopName);
		}
		// 前端展示的店铺都是审核成功的店铺
		shopCondition.setEnableStatus(1);
		return shopCondition;
	}
}
